package com.iflytek.controller;

import javax.servlet.http.HttpSession;

import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.IncorrectCredentialsException;
import org.apache.shiro.authc.UnknownAccountException;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.subject.Subject;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.iflytek.bean.Admin;
import com.iflytek.util.WebUtils;
import com.iflytek.vo.layuiPage;

@RestController
public class LoginController {
	  
	@RequestMapping("/login")
	public Object doLogin(String username, String password) {
		Subject subject = SecurityUtils.getSubject(); //subject对象是与Shiro提供的操作门面
    	UsernamePasswordToken token = new UsernamePasswordToken(username, password); //将用户名和密码封装成token
        
	    try {
	    	subject.login(token);  //执行认证
	        //获取登录用户信息
	        Admin admin = (Admin)subject.getPrincipal();
	      //将user存储到session中
            WebUtils.getSession().setAttribute("admin",admin);
            layuiPage result = new layuiPage();
    		result.setCode(0);
    		result.setMsg("登陆成功");
    		result.setData(admin);
    		return result;
	    }catch (UnknownAccountException e) {
	        System.out.println("帐号不存在,登录失败!");
	        layuiPage result = new layuiPage();
    		result.setCode(0);
    		result.setMsg("登陆失败");
    		return result;
	    }
	    
	}
}
